
module.exports = {
    /*
    * 授权手机号
    */
    AuthorizationPhone:{
        getPhoneNumber: function (e, callBack) { 
            var that = this;
            if (e && e.detail.errMsg == 'getPhoneNumber:ok') {
                let iv = e.detail.iv;
                let encryptedData = e.detail.encryptedData
                that.requestPhone(iv, encryptedData, callBack)
            } else {
                console.log("绑定手机号失败")
            }
        },
        requestPhone: function (iv, encryptedData, callBack) {
            let pages = getCurrentPages() //获取加载的页面
            let currentPage = pages[pages.length - 1] //获取当前页面的对象
            let pageurl = currentPage.route //当前页面url
            let optionss = currentPage.options
            getApp().request({
                url:wx.path.AUTHPHONE_NUMBER,
                method: 'post',
                data: {
                    iv: iv,
                    encryptedData: encryptedData,
                    page_path: pageurl,
                    page_path_parameter: optionss,
                },
                success: function (res) {
                    // console.log(res)
                    if(res.data.code==200){
                        wx.setStorageSync('token', res.data.data.token)
                        var token_expired = new Date().getTime()
                        token_expired += (1 * 86400 * 1000)
                        wx.setStorageSync('token_expired', token_expired)
                        wx.setStorageSync('user', res.data.data.user)
                        wx.setStorageSync('snsapi_userinfo', res.data.data.snsapi_userinfo)
                        wx.setStorageSync('snsapi_phonenumber', res.data.data.snsapi_phonenumber)
                        // console.log(getApp(),'getApp()')
                        getApp().globalData.UserInfoStatus = 1
                        getApp().globalData.UserInfoStatusCallback(res.data.data)
                        callBack.success(res)
                    }else {
                        wx.showToast({
                            title: res.data.errMsg,
                            icon: 'none',
                            duration: 1200,
                            mask: true
                        })
                    }
                    // console.log("绑定手机号成功")
                },
                fail: function (res) {
                    callBack.fail(res)
                    // console.log("绑定手机号失败")
                }
            })
        },
    },
    /*
    * 获取用户信息
    */
    AuthorizationUserInfo:{
        getUserInfoFun(cb) {
            let that = this
            wx.getUserProfile({
                desc: '用于完善会员资料', 
                success: function (res) {
                    that.requestUserInfo(res.rawData,res.signature, res.encryptedData, res.iv,(data)=>{
                        // that.globalData.UserInfoStatus = 1
                        // that.globalData.UserInfoStatusCallback(data)
                        typeof cb == "function" && cb(data)
                    })
                }
            })
        },
        requestUserInfo: function (rawData,signature, encryptedData, iv,cb) {
            getApp().request({
                data: {
                    rawData:rawData,
                    signature:signature,
                    encryptedData: encryptedData,
                    iv: iv,
                    token: wx.getStorageSync('token'),
                },
                method: 'POST',
                url: wx.path.AUTHUSERINFO,
                success: function (res) {
                    //console.log(res)
                    if (res.statusCode == '200' || res.data.code == '200') {
                         wx.setStorageSync('token', res.data.data.token)
                        var token_expired = new Date().getTime()
                        token_expired += (1 * 86400 * 1000)
                        wx.setStorageSync('token_expired', token_expired)
                        wx.setStorageSync('user', res.data.data.user)
                        wx.setStorageSync('snsapi_userinfo', res.data.data.snsapi_userinfo)
                        wx.setStorageSync('snsapi_phonenumber', res.data.data.snsapi_phonenumber)
                        typeof cb == "function" && cb(res.data.data)
                    } else {
                        wx.showToast({
                            title: res.data.errMsg,
                            icon: 'none',
                            duration: 1200,
                            mask: true
                        })
                    }
                },
                fail: function (res) {
                    // console.log("接口请求失败", res)
                }
            })
        },
        getUserInfoMessage(){
            var users = wx.getStorageSync('user')
            return users
        },
        // 是否有用户头像昵称信息
        getsnsapi_userinfo(){
            var snsapi_userinfo = wx.getStorageSync('snsapi_userinfo')
            return snsapi_userinfo?snsapi_userinfo:0
        },
        // 获取是否有手机号
        getsnsapi_phonenumber(){
            var snsapi_phonenumber = wx.getStorageSync('snsapi_phonenumber')
            return snsapi_phonenumber?snsapi_phonenumber:0
        },
        // 获取手机号
        getUserPhone(){
            var users = wx.getStorageSync('user')
            return users && users.phone_number?users.phone_number:''
        }
    },
    /**
     * 查询隐私授权情况
     */
    getPrivacySettingInfo:{
        getPrivacySettingInfoFun(cb) {
            wx.getPrivacySetting({
                success: res => {
                  console.log(res) // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
                  typeof cb == "function" && cb(res)
                },
                fail: () => {},
                complete:() => {}
            })
        },
       
    },
    /**
     * 
     */
    SystemInfo:{
        getSystemInfo() {
            var app = getApp();
            if (app.globalSystemInfo && !app.globalSystemInfo.ios) {
                return app.globalSystemInfo;
            } else {
                let systemInfo = wx.getSystemInfoSync();
                let ios = !!(systemInfo.system.toLowerCase().search('ios') + 1);
                let rect;
                try {
                    rect = wx.getMenuButtonBoundingClientRect ? wx.getMenuButtonBoundingClientRect() : null;
                    if (rect === null) {
                        throw 'getMenuButtonBoundingClientRect error';
                    }
                    //取值为0的情况  有可能width不为0 top为0的情况
                    if (!rect.width || !rect.top || !rect.left || !rect.height) {
                        throw 'getMenuButtonBoundingClientRect error';
                    }
                } catch (error) {
                    let gap = ''; //胶囊按钮上下间距 使导航内容居中
                    let width = 96; //胶囊的宽度
                    if (systemInfo.platform === 'android') {
                        gap = 8;
                        width = 96;
                    } else if (systemInfo.platform === 'devtools') {
                        if (ios) {
                            gap = 5.5; //开发工具中ios手机
                        } else {
                            gap = 7.5; //开发工具中android和其他手机
                        }
                    } else {
                        gap = 4;
                        width = 88;
                    }
                    if (!systemInfo.statusBarHeight) {
                        //开启wifi的情况下修复statusBarHeight值获取不到
                        systemInfo.statusBarHeight = systemInfo.screenHeight - systemInfo.windowHeight - 20;
                    }
                    rect = {
                        //获取不到胶囊信息就自定义重置一个
                        bottom: systemInfo.statusBarHeight + gap + 32,
                        height: 32,
                        left: systemInfo.windowWidth - width - 10,
                        right: systemInfo.windowWidth - 10,
                        top: systemInfo.statusBarHeight + gap,
                        width: width
                    };
                    //console.log('error', error);
                    //console.log('rect', rect);
                }
                let navBarHeight = '';
                if (!systemInfo.statusBarHeight) {
                    systemInfo.statusBarHeight = systemInfo.screenHeight - systemInfo.windowHeight - 20;
                    navBarHeight = (function () {
                        let gap = rect.top - systemInfo.statusBarHeight;
                        return 2 * gap + rect.height;
                    })();
                    systemInfo.statusBarHeight = 0;
                    systemInfo.navBarExtendHeight = 0; //下方扩展4像素高度 防止下方边距太小
                } else {
                    navBarHeight = (function () {
                        let gap = rect.top - systemInfo.statusBarHeight;
                        return systemInfo.statusBarHeight + 2 * gap + rect.height;
                    })();
                    if (ios) {
                        systemInfo.navBarExtendHeight = 4; //下方扩展4像素高度 防止下方边距太小
                    } else {
                        systemInfo.navBarExtendHeight = 0;
                    }
                }
                systemInfo.navBarHeight = navBarHeight; //导航栏高度不包括statusBarHeight
                systemInfo.capsulePosition = rect; //右上角胶囊按钮信息bottom: 58 height: 32 left: 317 right: 404 top: 26 width: 87 目前发现在大多机型都是固定值 为防止不一样所以会使用动态值来计算nav元素大小
                systemInfo.ios = ios; //是否ios
                app.globalSystemInfo = systemInfo; //将信息保存到全局变量中,后边再用就不用重新异步获取了
                return systemInfo;
            }
        }
    },
   
}